package org.parademo.dao;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.parademo.model.ProposalEntity;
import org.parademo.model.ProposalStatusEntity;
import org.parademo.service.model.ProposalStatusCode;

import java.util.List;

public class ProposalStatusDao extends GenericDao<ProposalEntity> {

    public ProposalStatusDao(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    @SuppressWarnings("unchecked")
    public List<ProposalStatusEntity> getStatuses() {
        Query query = getCurrentSession().createQuery("from ProposalStatusEntity");

        return query.list();
    }

    public void submitProposal(Long proposalId) {
        Query query = getCurrentSession().createQuery(
                "update\n" +
                "    ProposalEntity p\n" +
                "set\n" +
                "    p.status = (\n" +
                "        select s.id from ProposalStatusEntity s where s.code = :activeStatus \n" +
                "    )\n" +
                "where\n" +
                "    p.id = :proposalId\n\n"
        );

        query.setLong("proposalId", proposalId);
        query.setString("activeStatus", ProposalStatusCode.ACTIVE.name());

        query.executeUpdate();
    }
}
